package com.asd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.asd.dao.UserDao;
import com.asd.util.PageBean;
import com.asd.vo.User;
import com.google.gson.Gson;

/**
 * 完成用户的所有操作
 */
@WebServlet("/admin/stu")
public class UserServlet extends BaseServlet {
	UserDao dao = new UserDao();
	//添加用户
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		user.setUser_name(request.getParameter("name"));
		user.setUser_sex(request.getParameter("sex"));
		//user.setClass_id(Integer.parseInt(request.getParameter("classes"))); 
		user.setUser_phone(request.getParameter("phone"));
		user.setUser_wx(request.getParameter("wx"));
		int n = -1;
		try {
			n=dao.addUser(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(n);
	}
	
	//修改用户
		protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			System.out.println("update");
			User user = new User();
			user.setUser_name(request.getParameter("name")) ;
			user.setUser_sex(request.getParameter("sex"));
			user.setClass_id(Integer.parseInt(request.getParameter("classes"))); 
			user.setUser_phone(request.getParameter("phone"));
			user.setUser_wx(request.getParameter("wx"));
			user.setUser_id(Integer.parseInt(request.getParameter("uid")));
			System.out.println(user);
			int n = -1;
			try {
				n=dao.update(user);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(n);
			response.sendRedirect("/OnlineExam/admin/stu?m=page");
		}

	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("uid");
		int id = Integer.parseInt(uid);
		int n=-1;
		try {
			n=dao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(n);
		
		response.sendRedirect("/OnlineExam/admin/stu?m=page");
	}
	
	//查一条
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		System.out.println("查一条");
		int uid=Integer.parseInt(request.getParameter("uid"));
		User user=new User();
		try {
			user = dao.findByID(uid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(user);
		Gson gson = new Gson();
		PrintWriter out= response.getWriter();
		String str = gson.toJson(user);
		out.print(str);
		out.flush();
		out.close();
	}
	//查一页
	protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一页");
		
		String p =request.getParameter("p");
		
		if(p==null) {
			p="1";
		}
		
		int intp = Integer.parseInt(p);
		PageBean pb = new PageBean();
		List userList=null;
		try {
			userList = dao.show(intp);
			int count = dao.count();
			pb.setTotal(count);
			pb.setCurPage(intp);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//传给JSP 
		request.setAttribute("apage", userList);
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("/admin/student.jsp").forward(request, response);
	}

}
